﻿Public Class TestClass

    Public Sub Open()
        MsgBox("Open Command Received")
    End Sub

    Public Sub DisplayItem(ByVal item As Object)
        MsgBox(String.Format("The Selected Item is {0}", item.ToString))
    End Sub

    Public Function CanDisplayItem(ByVal item As Object) As Boolean
        Return item IsNot Nothing AndAlso TypeOf item Is String
    End Function

    Private Shared _Rnd As New Random
    Public Sub InvalidOperation()

        Dim value As Integer = _Rnd.Next(2)
        Dim adjective As String = String.Empty

        Select Case value
            Case 0
                adjective = "invalid"
            Case 1
                adjective = "stupid"
        End Select
        Dim message As String = String.Format("This operation is {0}!", adjective)
        Throw New InvalidOperationException(message)
    End Sub

    Public Sub LongRunningOperation()
        Debug.Print("Started LongRunningOperation")
        System.Threading.Thread.Sleep(5000)
    End Sub

    Private _TestItems As New System.Collections.ObjectModel.ObservableCollection(Of TestItem)
    Public ReadOnly Property TestItems() As System.Collections.ObjectModel.ObservableCollection(Of TestItem)
        Get
            Return _TestItems
        End Get
    End Property

    Public Property Count As Integer

    Public Sub DoFillItems(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)

        Dim worker As System.ComponentModel.BackgroundWorker = sender
        For i As Integer = 1 To Count
            Dim item As New TestItem(i, String.Format("Item {0}", i))
            If worker.CancellationPending Then
                e.Cancel = True
                Exit For
            End If
            worker.ReportProgress(100 * i / Count, item)
        Next
    End Sub

    Public Sub Reverse(ByVal item As Object)
        Dim i As TestItem = item
        i.Name = String.Join("", i.Name.Reverse)
        'System.Threading.Thread.Sleep(100)
    End Sub

End Class

Public Class TestItem
    Implements System.ComponentModel.INotifyPropertyChanged

    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            If _Name <> value Then
                _Name = value
                OnPropertyChanged("Name")
            End If
        End Set
    End Property

    Private _Number As Integer
    Public Property Number() As Integer
        Get
            Return _Number
        End Get
        Set(ByVal value As Integer)
            If _Number <> value Then
                _Number = value
                OnPropertyChanged("Number")
            End If
        End Set
    End Property

    Public Sub New(ByVal number As Integer, ByVal name As String)
        _Name = name
        _Number = number
        'System.Threading.Thread.Sleep(100)
    End Sub

    Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs(propertyName))
    End Sub

    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
